package com.sskj.gcc.ui.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.InputUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.rxbus.AccountBean;
import com.sskj.gcc.presenter.RegisterActivityPresenter;
import com.sskj.gcc.util.PhoneNumSelectDialog;
import com.sskj.gcc.util.PhoneNumSelectListener;
import com.sskj.gocoin.v.activity.WebViewActivity;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.TabItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.APP_REGISTER)//注册
public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> {
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.ivAccountClear)
    ImageView ivAccountClear;
    @BindView(R.id.etCode)
    EditText etCode;
    @BindView(R.id.tvGetCheckCode)
    TextView tvGetCheckCode;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R.id.et_register_invite)
    EditText etInviteCode;
    @BindView(R.id.cb_register_protocol)
    CheckBox checkBox;
    @BindView(R.id.btNext)
    Button btNext;
    @BindView(R.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.tvAgree)
    TextView tvAgree;
    private boolean isMobile = true;
    private Disposable timeDis;
    private AndroidNextInputs inputs;
    private AndroidNextInputs inputsCheckCode;

    private String phoneNum = "86";
    private String phoneName = App.INSTANCE.getString(R.string.app_pwdForgetActivity1);

    private boolean isSee = false;
    private boolean isSeeAgain = false;

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_register;
    }

    @Override
    public RegisterActivityPresenter getPresenter() {
        return new RegisterActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.appJavaRegisterActivity1));
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.appJavaLoginActivity2), 0, 0));
        tabItems.add(new TabItem(App.INSTANCE.getString(R.string.appJavaLoginActivity3), 0, 0));
        commonTabLayout.setTabData(tabItems);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position) {
                    case 0:
                        if (isMobile)
                            return;
                        changeMobile();
                        break;
                    case 1:
                        if (!isMobile)
                            return;
                        changeEmail();
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        inputs = new AndroidNextInputs();
        inputs.clear();
        inputs.add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank());

        inputsCheckCode = new AndroidNextInputs();
        inputsCheckCode.clear();
        inputsCheckCode
                .add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank());

        ClickUtil.click(btNext, () -> {
            if (isMobile) {
                if (inputs.test()) {
                    inputs.clear();
                    inputs.add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile())
                            .add(etCode, StaticScheme.NotBlank())
                            .add(etPwd, StaticScheme.NotBlank())
                            .add(etPwdAgain, StaticScheme.NotBlank());
                    if (!CheckUtil.isPassWord(etPwd.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appRegisterPwdHint));
                        return;
                    }
                    if (!TextUtils.isEmpty(etPwd.getText().toString()) && !TextUtils.isEmpty(etPwdAgain.getText().toString())) {
                        if (!etPwd.getText().toString().equals(etPwdAgain.getText().toString())) {
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity5));
                            return;
                        }
                    }
                    if (TextUtils.isEmpty(etInviteCode.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appXmlapp_activity_register_next94));
                        return;
                    }
                    if (!checkBox.isChecked()) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity2));
                        return;
                    }
                    mPresenter.checkCode(etAccount.getText().toString(), etCode.getText().toString());
//                    checkCodeSuccess();
                }
            } else {
                if (inputsCheckCode.test()) {
                    inputsCheckCode.clear();
                    inputsCheckCode
                            .add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email())
                            .add(etCode, StaticScheme.NotBlank())
                            .add(etPwd, StaticScheme.NotBlank())
                            .add(etPwdAgain, StaticScheme.NotBlank());
                    if (!CheckUtil.isPassWord(etPwd.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appRegisterPwdHint));
                        return;
                    }
                    if (!TextUtils.isEmpty(etPwd.getText().toString()) && !TextUtils.isEmpty(etPwdAgain.getText().toString())) {
                        if (!etPwd.getText().toString().equals(etPwdAgain.getText().toString())) {
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity5));
                            return;
                        }
                    }
                    if (TextUtils.isEmpty(etInviteCode.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appXmlapp_activity_register_next94));
                        return;
                    }
                    if (!checkBox.isChecked()) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity2));
                        return;
                    }
                    mPresenter.checkCode(etAccount.getText().toString(), etCode.getText().toString());
//                    checkCodeSuccess();
                }
            }
        });
        ClickUtil.click(ivAccountClear, () -> {
            etAccount.setText("");
        });
        ClickUtil.click(tvAgree, () -> {
            WebViewActivity.start(this, "reg_agree");
        });
        ClickUtil.click(tvGetCheckCode, () -> {
            inputs.clear();
            inputs.add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile());
            inputsCheckCode.clear();
            inputsCheckCode.add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email());
            if (isMobile) {
                if (inputs.test()) {
                    tvGetCheckCode.setEnabled(false);
                    mPresenter.sendCode(etAccount.getText().toString(), phoneNum);
                }
            } else {
                if (inputsCheckCode.test()) {
                    tvGetCheckCode.setEnabled(false);
                    mPresenter.sendCode(etAccount.getText().toString(), phoneNum);
                }
            }

        });

        ClickUtil.click(200, ivPwdToggle, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivPwdToggle.setImageResource(R.mipmap.app_icon_hide);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivPwdToggle.setImageResource(R.mipmap.app_icon_show);
            }
        });

        ClickUtil.click(200, ivPwdToggleAgain, () -> {
            if (isSeeAgain) {
                isSeeAgain = false;
                etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivPwdToggleAgain.setImageResource(R.mipmap.app_icon_hide);
            } else {
                isSeeAgain = true;
                etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivPwdToggleAgain.setImageResource(R.mipmap.app_icon_show);
            }
        });
    }

    private void changeEmail() {
        isMobile = false;
        etCode.setText("");
        etAccount.setText("");
        etAccount.setHint(App.INSTANCE.getString(R.string.app_register_email_hint));
        inputs.clear();
        inputsCheckCode
                .add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank());
        tvType.setText(App.INSTANCE.getString(R.string.appJavaLoginActivity8));
    }

    private void changeMobile() {
        isMobile = true;
        etAccount.setText("");
        etCode.setText("");
        etAccount.setHint(App.INSTANCE.getString(R.string.appJavaRegisterActivity8));
        inputs.clear();
        inputs.add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank());
        tvType.setText(App.INSTANCE.getString(R.string.appJavaLoginActivity4));
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        tvGetCheckCode.setText(aLong + "s");
                    } else {
                        tvGetCheckCode.setText(App.INSTANCE.getString(R.string.appJavaRegisterActivity10));
                        tvGetCheckCode.setEnabled(true);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        tvGetCheckCode.setEnabled(true);
    }

    public void checkCodeSuccess() {
        inputs.clear();
        inputs.add(etAccount, StaticScheme.NotBlank(), InputUtil.Mobile())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank());
        inputsCheckCode.clear();
        inputsCheckCode
                .add(etAccount, StaticScheme.NotBlank(), StaticScheme.Email())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank())
                .add(etPwdAgain, StaticScheme.NotBlank());

        if (isMobile) {
            if (inputs.test()) {
                if (!CheckUtil.isPassWord(etPwd.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appRegisterPwdHint));
                    return;
                }
                if (!TextUtils.isEmpty(etPwd.getText().toString()) && !TextUtils.isEmpty(etPwdAgain.getText().toString())) {
                    if (!etPwd.getText().toString().equals(etPwdAgain.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity5));
                        return;
                    }
                }
                if (TextUtils.isEmpty(etInviteCode.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appXmlapp_activity_register_next94));
                    return;
                }
                if (!checkBox.isChecked()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity2));
                    return;
                }
                mPresenter.register("",etAccount.getText().toString(), phoneNum, etCode.getText().toString(),
                        etPwd.getText().toString(),
                        etPwdAgain.getText().toString(),
                        etInviteCode.getText().toString()
                );
            }
        } else {
            if (inputsCheckCode.test()) {
                if (!CheckUtil.isPassWord(etPwd.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appRegisterPwdHint));
                    return;
                }
                if (!TextUtils.isEmpty(etPwd.getText().toString()) && !TextUtils.isEmpty(etPwdAgain.getText().toString())) {
                    if (!etPwd.getText().toString().equals(etPwdAgain.getText().toString())) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity5));
                        return;
                    }
                }
                if (TextUtils.isEmpty(etInviteCode.getText().toString())) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appXmlapp_activity_register_next94));
                    return;
                }
                if (!checkBox.isChecked()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.appJavaRegisterNextActivity2));
                    return;
                }
                mPresenter.register(etAccount.getText().toString(), "",phoneNum, etCode.getText().toString(),
                        etPwd.getText().toString(),
                        etPwdAgain.getText().toString(),
                        etInviteCode.getText().toString()
                );
            }
        }
//        ARouter.getInstance().build(RConfig.APP_REGISTER_NEXT)
//                .withString(Constans.CODE, etCode.getText().toString())
//                .withString(Constans.ACCOUNT, etAccount.getText().toString())
//                .navigation();
//        finish();
    }

    public void registerSuccess() {
        RxBus.getDefault().post(new AccountBean(etCode.getText().toString()));
        finish();
    }
}
